package com.example.carddemo.slice;

import com.example.carddemo.MainAbility;
import com.example.carddemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentProvider;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                long formCardId =  ((MainAbility) getAbility()).formId;
                if (formCardId != -1) {
                    ProviderFormInfo formInfo = new ProviderFormInfo(ResourceTable.Layout_form_image_with_info_widget_2_2, getContext());
                    ComponentProvider componentProvider = formInfo.getComponentProvider();
                    componentProvider.setText(ResourceTable.Id_widget_titlt_text, "更新后的卡片标题");
                    componentProvider.setText(ResourceTable.Id_widget_titlt_desc, "更新后的卡片描述文案");

                    try {
                        getAbility().updateForm(formCardId, componentProvider);
                    } catch (FormException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
